package com.sanyang.lawyer.controller.client;

import com.sanyang.lawyer.entity.*;
import com.sanyang.lawyer.service.client.ClientService;
import com.sanyang.lawyer.service.hr.HrEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("client")
public class ClientController {

    @Autowired
    private ClientService clientService;
    @Autowired
    private HrEmpService hrEmpService;
    //查询出所有我的客户
    @RequestMapping("mylistclientall")
    public Map<String,Object> listall(Client client,HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        String clientassign = request.getParameter("cAssign1");
        String clientname = request.getParameter("cName1");
        String clientphone = request.getParameter("cPhone1");
        String clientregion = request.getParameter("cRegion1");
        String clienttype = request.getParameter("cType1");
        String empId = request.getParameter("empid");
        String empname = request.getParameter("empname");
        Clientpage clientpage = new Clientpage(client,request);
        map.put("pagetext",clientpage);
        if(empId!=null&&empId!=""){
            map.put("empId",Integer.parseInt(empId));
        }
        if(empname!=null&&empname!=""){
            List<Hr_emp> emp = hrEmpService.getByEmpName(empname);
            map.put("empId",emp.get(0).getEmpId());
        }
        if (clientname != null&&clientname!="") {
            map.put("clientname",clientname);
        }
        if (clientphone != null&&clientphone!="") {
            map.put("clientphone",clientphone);
        }
        if (clientregion != null&&clientregion!="") {
            map.put("clientregion",clientregion);
        }
        if (clientassign != null&&clientassign!="") {
            switch (clientassign) {
                case "k":
                    map.put("clientassign", "null");
                    System.out.println("assign:"+clientassign);
                    break;
                case "y":
                    map.put("clientassign", "已");
                    System.out.println("assign:"+clientassign);
                    break;
                case "n":
                    map.put("clientassign", "未");
                    System.out.println("assign:"+clientassign);
                    break;
            }
        }
        //客户类型
        if (clienttype != null&&clienttype!="") {
            switch (clienttype){
                case "0":
                    map.put("clienttype","");
                    break;
                case "1":
                    int  typeid = clientService.getbytypename("公民个人");
                    map.put("clienttype",typeid);
                    break;
                case "2":
                      typeid = clientService.getbytypename("机关单位");
                    map.put("clienttype",typeid);
                    break;
                case "3":
                      typeid = clientService.getbytypename("公司企业");
                    map.put("clienttype",typeid);
                    break;
                case "4":
                      typeid = clientService.getbytypename("社会团体、社会组织");
                    map.put("clienttype",typeid);
                    break;
                case "5":
                      typeid = clientService.getbytypename("其他");
                    map.put("clienttype",typeid);
                    break;
            }
        }
        List<Client> clients = clientService.listallpage(map);
        Map<String,Object> maps = new HashMap<String,Object>();
        Integer count = clientService.getcount(map);
        if(clients.size()<=0){
            count=0;
            maps.put("code",0);
            maps.put("msg","");
            maps.put("count",count);
            maps.put("data","");
        }else{
            maps.put("code",0);
            maps.put("msg","");
            maps.put("count",count);
            maps.put("data",clients);
        }
        return maps;
    }
    //通过客户信息编号查询出客户信息
    @RequestMapping("getbymessageid")
    public Client_message getbymessageid(int clientId){
        Client client = clientService.getbyid(clientId);
        Client_message client_message = clientService.getbymessageid(client.getMessageId().getMessageId());
        return client_message;
    }


    //通过客户类型编号查询类型
    @RequestMapping("type")
    @ResponseBody
    public Client_type type(int type){
        Client_type clienttype = clientService.getbytypeid(type);
        return clienttype;
    }
    @RequestMapping("typeall")
    @ResponseBody
    public List<Client_type> typeall(){
        List<Client_type> clienttype = clientService.listtypeall();
        return clienttype;
    }

    //查询所有的类型
    @RequestMapping("liststateall")
    public List<Client_type>  liststateall(){
        List<Client_type> list=clientService.liststateall();
        return list;
    }
    //查询所有对方当事人
    @RequestMapping("listprivyall")
    public Map<String,Object> listprivyall(Client client,HttpServletRequest request){
        Clientpage clientpage = new Clientpage(client,request);
        String clientname = request.getParameter("cName1");
        String empId = request.getParameter("empId");
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("pagetext",clientpage);
        maps.put("clientname",clientname);
        maps.put("empId",Integer.parseInt(empId));
        System.out.println("clientname:"+clientname);
        List<Client> clients = clientService.listprivyall(maps);
        Map<String,Object> map = new HashMap<String,Object>();
        Integer count = clientService.getcount(maps);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",clients);
        return map;
    }
    //修改对方当事人信息
    @RequestMapping("updateclient")
    public String updateclient(int clientId,String clientName){
            Client client = new Client();
            client.setClientId(clientId);
            client.setClientName(clientName);
            clientService.updateclient(client);
        return "succeed";
    }

}
